/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package morseSide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Allan Helbling
 */
public class SocketIn {

    private Socket socket;
    BufferedReader in;
    PrintWriter out;

    public SocketIn(InetAddress ip, int port) {
        try {
            //socket = new Socket(InetAddress.getLocalHost(), 60000); // 60000 est le premier capteur, 60001 le second ...;
            setSocket(new Socket(ip, port));
        } catch (IOException ex) {
            System.out.println("Problème lors de la création du socketIn");
        }

    }

    public String getMessage() {
        String message_distant = "";

        try {
            in = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));
            message_distant = in.readLine();

        } catch (IOException ex) {
            Logger.getLogger(SocketIn.class.getName()).log(Level.SEVERE, null, ex);
        }
        return message_distant;
    }

    public void close() {
        try {
            getSocket().close();
        } catch (IOException ex) {
            System.out.println("Problème lors de la fermeture du socketIn sur l'adresse " + getSocket().getInetAddress() + "sur le port" + getSocket().getPort());
        }
    }

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}
}
